﻿@page "/RecordReview"
@using System.ComponentModel.DataAnnotations
@using System.ComponentModel
@using System.Text.Json
@using AntDesign.TableModels

@layout TeacherLayout
@inject TeacherClient Http
@inject NavigationManager Navigation
@inject IJSRuntime JS

<PageTitle>审核列表</PageTitle>

@if (@loading)
{
    <p>Loading...</p>

}
else
{
    <div style="margin:0 0 10px 5px;">
        <Button Loading=@_downloading Type=@ButtonType.Primary @onclick="DownloadUnfinishedStudents">
            导出未完成学生列表
        </Button>
        <Button Loading=@_downloading Type=@ButtonType.Primary @onclick="DownloadFinishedStudents">
            导出已完成学生信息
        </Button>
    </div>
    <Table @ref="table"
        TItem="RecordReviewFront"
        DataSource="@ReviewFront"
        Total="_total"
        @bind-PageIndex="_pageIndex"
        @bind-PageSize="_pageSize"
        @bind-SelectedRows="selectedRows">
        <Selection Key="@(context.Id.ToString())" />
        <Column @bind-Field="@context.Sno" Sortable />
        <Column @bind-Field="@context.Time" Format="yyyy-MM-dd" Sortable />
        <Column Title="学生名字" Field="@context.Name" />
        <Column Title="旧宿舍" Field="@context.OldDoom"/>
        <Column Title="新宿舍" Field="@context.NewDoom"/>
        <Column Title="旧校外地址" Field="@context.OldAddress"/>
        <Column Title="新校外地址" Field="@context.NewAddress"/>
        <ActionColumn>
            <Space>
                <SpaceItem><Button Danger OnClick="()=>{submitId = context.Id;isAccept = false;_reject_visible = true;toDelete = context;}">驳回请求</Button></SpaceItem>
            </Space>
            <Space>
                <SpaceItem><Button Danger OnClick="()=>{submitId = context.Id;isAccept = true;_accept_visible = true;toDelete = context;}">通过请求</Button></SpaceItem>
            </Space>
        </ActionColumn>
    </Table>
    <Modal Title="@("请输入通过请求原因")"
       Visible="@_accept_visible"
       OnOk="@SubmitReason"
       OnCancel="@CloseModal"
       OkText="@("确认接受")"
       CancelText="@("取消")" 
       ConfirmLoading="@_confirmLoading">
        <Input @bind-Value="@submitString" />
    </Modal>
    <Modal Title="@("请输入驳回请求原因")"
       Visible="@_reject_visible"
       OnOk="@SubmitReason"
       OnCancel="@CloseModal"
       OkText="@("确认驳回")"
       CancelText="@("取消")"
       ConfirmLoading="@_confirmLoading">
        <Input @bind-Value="@submitString" />
    </Modal>
    <Modal Title="@("请输入理由!")"
       Visible="@_error_visable"
       OnOk="@CloseError"
       OnCancel="@CloseError">
    </Modal>
}



@code {
    ITable? table;
    private bool loading = true;
    IList<RecordReview>? records;
    List<RecordReviewFront>? ReviewFront;
    IEnumerable<RecordReviewFront>? selectedRows;

    int _pageIndex = 1;
    int _pageSize = 10;
    int _total = 0;

    RecordReviewFront toDelete = new RecordReviewFront();
    bool isAccept;
    bool _accept_visible = false;
    bool _reject_visible = false;
    bool _error_visable = false;

    bool _downloading = false;

    string? submitString;
    int submitId;
    bool _confirmLoading = false;

    protected async override Task OnInitializedAsync()
    {
        records = await Http.GetRecordReview();

        ReviewFront = records.Select(record => new RecordReviewFront
                                        {
                                            Id = record.Id,
                                            Time = record.Time,
                                            Sno = record.Sno,
                                            Name = record.Name,
                                            OldDoom = record.OldDoom,
                                            NewDoom = record.NewDoom,
                                            OldAddress = record.OldAddress,
                                            NewAddress = record.NewAddress
                                        })
                              .ToList();
        _total = records.Count;
        loading = false;
    }
    public class RecordReviewFront
    {
        public int Id { get; set; }

        [DisplayName("日期")]
        public DateTime Time { get; set; }

        [DisplayName("学号")]
        public string Sno { get; set; } = null!;

        [DisplayName("学生名字")]
        public string Name { get; set; } = null!;

        [DisplayName("原宿舍")]
        public string? OldDoom { get; set; } // 例如：学海2舍-302  西14-321

        [DisplayName("修改后宿舍")]
        public string? NewDoom { get; set; }

        [DisplayName("原校外住宿地址")]
        public string? OldAddress { get; set; } 

        [DisplayName("修改后校外住宿地址")]
        public string? NewAddress { get; set; }
    }

    public void CloseModal()
    {
        _error_visable = false;
        _accept_visible = false;
        _reject_visible = false;
    }

    public void CloseError()
    {
        _error_visable = false;
    }

    public async Task SubmitReason()
    {
        _confirmLoading = true;
        if (string.IsNullOrWhiteSpace(submitString))
        {
            _error_visable = true;
            _confirmLoading = false;
            return;
        }
        else
        {
            await Http.ReviewRequest(submitId, isAccept, submitString);
            _confirmLoading = false;
            CloseModal();
            ReviewFront!.Remove(toDelete);
        }
    }

    public async Task DownloadUnfinishedStudents()
    {
        _downloading = true;
        var stream = await Http.GetUnfinishedStudents();
        var fileName = $"未完成学生列表{DateTime.Now:MMddHHmm}.xlsx";
        using var streamRef = new DotNetStreamReference(stream: stream);

        await JS.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
        _downloading = false;
    }

    public async Task DownloadFinishedStudents()
    {
        _downloading = true;
        var stream = await Http.GetFinishedStudents();
        var fileName = $"已核对学生信息{DateTime.Now:MMddHHmm}.xlsx";
        using var streamRef = new DotNetStreamReference(stream: stream);

        await JS.InvokeVoidAsync("downloadFileFromStream", fileName, streamRef);
        _downloading = false;
    }
}

